/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.diary;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.unis.common.util.TemplateUtil;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.diary.Diary;

/**
 * @author Administrator (email:357375084@qq.com)
 * @since 2013-02-25
 */
@Service("diaryService")
public class DiaryServiceImpl implements DiaryService {

    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of Diary
     ****************************************/
    public void insert(Diary diary) throws Exception {
        diary.setDiaryid(TemplateUtil.genUUID());
        iDataSet4iBatis.insert(diary);
    }

    /****************************************
     * Update a instace of Diary
     ****************************************/
    public void update(Diary diary) throws Exception {
        iDataSet4iBatis.update(diary);
    }

    /****************************************
     * Delete a instace of Diary
     ****************************************/
    public void delete(Diary diary) throws Exception {
        iDataSet4iBatis.delete(diary);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(Diary diary) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(Diary diary) throws Exception {
        String statementId = diary.getStatmentId();
        String mainId = diary.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> diaryList = new ArrayList<BaseModel>();
        for (String id : ids) {
            Diary model = new Diary();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            diaryList.add(model);
        }
        iDataSet4iBatis.batchDelete(diaryList);
    }

    /****************************************
     * Query instaces of Diary
     ****************************************/
    public List<BaseModel> queryAsList(Diary diary) throws Exception {
        String queryListStatement = "";
        if (diary.isPaging()) {
            queryListStatement = diary.getStatmentId();
            diary.setStatmentId("Diary.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(diary);
            diary.setTotalCount(baseModel.getTotalCount());
            diary.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(diary);
    }

    /****************************************
     * Query a instaces of Diary
     ****************************************/
    public BaseModel queryAsObject(Diary diary) throws Exception {
        return iDataSet4iBatis.queryAsObject(diary);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
}
